/**
 * 	 Copyright (c) 2008, 2009 Lukas Zaruba
 * 
 *   This file is part of UniAnalyzer (Universal Analyzer).
 *
 *   UniAnalyzer is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   UniAnalyzer is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public License
 *   along with UniAnalyzer. If not, see <http://www.gnu.org/licenses/>.
 */


package net.zarubsys.unianalyzer.exporter.binary;

import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Text;

/**
 * OutputSelectionPage
 *
 * @author  &lt;A HREF=&quot;mailto:lukas.zaruba@gmail.com&quot;&gt;Lukas Zaruba&lt;/A&gt
 * @version $Revision$ $Date$
 */
public class OutputSelectionPage extends WizardPage {

	private static final String SUFFIX_1 = ".BIN";
	private static final String SUFFIX = ".bin";
	private Text text;

	protected OutputSelectionPage(String pageName) {
		super(pageName);
	}

	public void createControl(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		composite.setLayout(new GridLayout(2, false));
		
		text = new Text(composite, SWT.SINGLE | SWT.LEAD | SWT.BORDER);
		text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
		
		text.addModifyListener(new ModifyListener() {

			public void modifyText(ModifyEvent e) {
				validate();
			}
			
		});
		
		Button button = new Button(composite, SWT.PUSH);
		button.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
		button.setText("Select...");
		
		button.addSelectionListener(new SelectionListener() {

			public void widgetDefaultSelected(SelectionEvent e) {
				// ignore
			}

			public void widgetSelected(SelectionEvent e) {
				FileDialog fd = new FileDialog(Display.getCurrent().getActiveShell(), SWT.SAVE);
		        fd.setText("Save");
		        String open = fd.open();
		        if (!open.endsWith(SUFFIX) || !open.endsWith(SUFFIX_1)) {
		        	open += SUFFIX;
		        }
				text.setText(open);
				validate();
			}
			
		});
		
		setControl(parent);
		validate();
	}
	
	void validate() {
		if (text.getText().length() == 0) {
			setOutput("Name cannot be empty");
		} else if ((!text.getText().endsWith(SUFFIX) && !text.getText().endsWith(SUFFIX_1))) {
			setOutput("Name has to end with the suffix \".bin\" or \".BIN\"");
		} else {
			setOutput(null);
		}
	}
	
	public String getPath() {
		return text.getText();
	}
	
	void setOutput(String text) {
		setErrorMessage(text);
		if (text == null) {
			setPageComplete(true);
		} else {
			setPageComplete(false);
		}
	}

}


/*
 * $Log$
 */